package com.furentech.iotserver.fa.sinopec.sinopec_processor;

import com.alibaba.fastjson.JSON;
import com.furentech.iotserver.fa.sinopec.SinopecFunCode;
import com.furentech.iotserver.fa.utils.HexStringUtils;
import com.furentech.iotserver.fa.utils.NumberUtil;
import io.netty.channel.Channel;
import lombok.Data;
import org.springframework.stereotype.Component;

@Data
class NozzleDownData {
    int NZN;
    long POS_TTC;
    String Dispenser_TIME;
    String  G_CODE;
    byte Pay_X;
    int PRC;
    int CTC;
    long V_TOT;
    int AMN;
    int VOL;
    String ASN;
    String MAC;
}


@Component
public class NozzleDownData_Processor extends SinopecProcessorBase{
    NozzleDownData decodeFormBytes(byte[] buf) {
        NozzleDownData nozzleDownData = new NozzleDownData();

        nozzleDownData.setNZN(buf[1]);
        nozzleDownData.setPOS_TTC(NumberUtil.byte2intBE(buf, 2, 4));
        nozzleDownData.setDispenser_TIME(HexStringUtils.toHexString(buf, 6, 7));
        nozzleDownData.setG_CODE(HexStringUtils.toHexString(buf, 13, 2));
        nozzleDownData.setPay_X(buf[15]);
        nozzleDownData.setPRC((int)NumberUtil.byte2intBE(buf, 16, 2));
        nozzleDownData.setCTC((int) NumberUtil.byte2intBE(buf, 18, 2));
        nozzleDownData.setV_TOT(NumberUtil.byte2intBE(buf, 20, 4));
        nozzleDownData.setAMN((int) NumberUtil.byte2intBE(buf, 24, 3));
        nozzleDownData.setVOL((int) NumberUtil.byte2intBE(buf, 27, 3));
        nozzleDownData.setASN(HexStringUtils.toHexString(buf, 30, 10));
        nozzleDownData.setMAC(HexStringUtils.toHexString(buf, 40, 4));

        return nozzleDownData;
    }

    @Override
    String actualGetJsonString(byte[] buf) {
        NozzleDownData nozzleDownData =  decodeFormBytes(buf);
        if (nozzleDownData == null) {
            return null;
        }
        return JSON.toJSONString(nozzleDownData);
    }

    @Override
    String actualGetReadableString(byte[] buf) {

        NozzleDownData nozzleDownData =  decodeFormBytes(buf);

        return String.format(
                "枪号：     %d\r\n" +
                        "交易流水号：%d\r\n" +
                        "加油机时间：%s\r\n" +
                        "油品代码：  %s\r\n" +
                        "付款方式：  %02x\r\n" +
                        "价格：  %8.2f\r\n" +
                        "卡交易流水：%d\r\n" +
                        "升累计：%8.2f\r\n" +
                        "金额：  %8.2f\r\n" +
                        "升数：  %8.2f\r\n" +
                        "卡号：      %s\r\n" +
                        "消息鉴别码： %s\r\n"
                ,
                nozzleDownData.getNZN(),
                nozzleDownData.getPOS_TTC(),
                nozzleDownData.getDispenser_TIME(),
                nozzleDownData.getG_CODE(),
                nozzleDownData.getPay_X(),
                (float) nozzleDownData.getPRC() / 100.0,
                nozzleDownData.getCTC(),
                (float) nozzleDownData.getV_TOT() / 100.0,
                (float) nozzleDownData.getAMN() / 100.0,
                (float) nozzleDownData.getVOL() / 100.0,
                nozzleDownData.getASN(),
                nozzleDownData.getMAC()
        );
    }

    @Override
    byte[] actualGetResponse(byte[] buf, Channel channel) {

        NozzleDownData nozzleDownData =  decodeFormBytes(buf);

        byte[] resByteBuf = new byte[32];

        resByteBuf[0] = SinopecFunCode.Payment_Data;
        System.arraycopy(buf, 1, resByteBuf, 1, 5);
        resByteBuf[6] = buf[15];

        System.arraycopy(buf, 24, resByteBuf, 7, 3);
        System.arraycopy(buf, 24, resByteBuf, 10, 3);
        System.arraycopy(buf, 24, resByteBuf, 13, 3);

        resByteBuf[16] = 0x00;
        resByteBuf[17] = 0x00;

        System.arraycopy(buf, 30, resByteBuf, 18, 10);
        System.arraycopy(buf, 40, resByteBuf, 21, 4);

        return resByteBuf;
    }
}
